package cn.hefan.web.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class LoginFilter implements Filter {

	public void destroy() {

	}

	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
       //强转
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse res = (HttpServletResponse)response;
		//1.获取资源请求路径
		String uri = req.getRequestURI();
		//2.判断是否包含登陆相关资源路径
		if(uri.contains("/login.html")||uri.contains("/login")||uri.contains("/js/")||uri.contains("/css/")||uri.contains("/img/")||uri.contains("/fonts/")||uri.contains("/images/")) {
			//包含，用户想登陆
			chain.doFilter(request, response);
		}else {
			//不包含，需要验证用户是否登陆
			//3.从获取session中获取user
			Object admin = req.getSession().getAttribute("admin");
			if(admin !=null) {
				chain.doFilter(request, response);
			}else {
				//没有登陆，跳转登陆页面
				res.sendRedirect("/DataAcp/login.html");
			}
		}
		
		
		//chain.doFilter(request, response);
	}

	
	public void init(FilterConfig fConfig) throws ServletException {

	}

}
